package com.gjy.scheduling.monitor.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gjy.scheduling.monitor.entity.CfgMonitorInfo;
import com.gjy.scheduling.monitor.mapper.CfgMonitorInfoMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

/**
 * @author gjy
 * @version 1.0
 * @since 2025-07-12 10:32:44
 */
@RestController
@RequestMapping("/cfgMonitorInfo")
public class CfgMonitorInfoController {

    @Resource
    private CfgMonitorInfoMapper cfgMonitorInfoMapper;
    @Resource
    private DataSource dataSource;

    private static final Integer BATCH_SIZE = 1;
    private static final String FILE_PATH_XLSX = "D:\\code\\java\\microservice_spc\\utils-all\\spring-scheduling-monitor\\src\\main\\resources\\file\\";

    @GetMapping("/exportCursor")
    public void exportCursor() {

        try (Connection connection = dataSource.getConnection();
             Statement cs = connection.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
             ExcelWriter excelWriter = EasyExcel.write(FILE_PATH_XLSX + "cursor.xlsx", CfgMonitorInfo.class).build()) {
            cs.setFetchSize(1);
            WriteSheet sheet = EasyExcel.writerSheet("用户数据").build();

            try (ResultSet rs = cs.executeQuery("select monitor_id, monitor_desc, monitor_type, cron, execute_command, execute_type, expect_value, is_valid, alarm_content, email_title, process_id from cfg_monitor_info;")) {
                List<CfgMonitorInfo> buffer = new ArrayList<>(BATCH_SIZE);
                while (rs.next()) {
                    CfgMonitorInfo info = new CfgMonitorInfo();
                    info.setMonitorId(rs.getLong("monitor_id"));
                    info.setMonitorDesc(rs.getString("monitor_desc"));
                    info.setMonitorType(rs.getInt("monitor_type"));
                    info.setCron(rs.getString("cron"));
                    info.setExecuteCommand(rs.getString("execute_command"));
                    info.setExecuteType(rs.getInt("execute_type"));
                    info.setExpectValue(rs.getString("expect_value"));
                    info.setIsValid(rs.getInt("is_valid"));
                    info.setAlarmContent(rs.getString("alarm_content"));
                    info.setEmailTitle(rs.getString("email_title"));
                    info.setProcessId(rs.getInt("process_id"));
                    buffer.add(info);
                    if (buffer.size() >= BATCH_SIZE) {
                        excelWriter.write(buffer, sheet);
                        buffer.clear();
                    }

                }
                if (!buffer.isEmpty()) {
                    excelWriter.write(buffer, sheet); // 写入剩余数据
                }
                buffer = null; // 释放内存
            } catch (Exception e) {
                throw new RuntimeException(e);
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @GetMapping("/exportSimple")
    public void exportSimple() {
        try (ExcelWriter excelWriter = EasyExcel.write(FILE_PATH_XLSX + "simple.xlsx", CfgMonitorInfo.class).build();) {

            List<CfgMonitorInfo> cursor = cfgMonitorInfoMapper.selectList(new QueryWrapper<>());
            WriteSheet sheet = EasyExcel.writerSheet("用户数据").build();
            excelWriter.write(cursor, sheet); // 写入剩余数据
        }
    }

}
